<template>
  <!-- 近义词 -->
  <div class="app-container">
    <!-- <h1>近义词</h1> -->
    <el-button
      type="primary"
      @click="dialogVisible = true"
      v-hasPermi="['base:synonym:add']"
    >
      添加</el-button
    >
    <div class="boxlaw">
      <div class="comform">
        <ComForm
          ref="comForm"
          @EventReset="resetFn"
          @EventOnsubmit="queryFn"
          :list="comForm"
        >
        </ComForm>
      </div>

      <ComTable
        @handleCurrentChange="CurrentPageChangeFn"
        @handleSizeChange="currentPageSizeChangeFn"
        :key="random"
        :list="tableList"
        :modes="tableModes"
        :config="{ border: true, loading }"
        :pagination="{ total: paging.total, pageNum: paging.pageNum }"
      >
        <el-table-column width="200" #default="{ row }" align="left">
          <template>
            <el-button
              @click="editFn(row)"
              plain
              size="mini"
              v-hasPermi="['base:synonym:edit', 'base:synonym:getInfo']"
              type="primary"
              >修改</el-button
            >
            <el-button
              v-hasPermi="['base:synonym:remove']"
              @click="deleteFn(row)"
              plain
              size="mini"
              type="primary"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </ComTable>
      <AddAndEdit
        :isEdit="isEdit"
        @EventisEdit="ChangeIsEditFn"
        ref="AddAndEdit"
        @EventgetData="addOrEditFn"
        :dialogVisible.sync="dialogVisible"
      ></AddAndEdit>
    </div>
  </div>
</template>

<script>
import ComForm from "../../../components/comForm.vue";

import ComTable from "../../../components/comTable.vue";
import comForm from "../../../components/comForm.vue";

import {
  getSynonymListAPi,
  getSynonymRemoveAPi,
  getSynonymAddAPi,
  getSynonymEditAPi,
} from "@/wss/api/ContentSupervision/similar.js";
import AddAndEdit from "./addAndEdit.vue";

export default {
  name: "laws",
  dicts: ["sys_normal_disable"],
  components: { ComForm, comForm, ComTable, AddAndEdit },

  data() {
    return {
      loading: false,
      paging: { pageNum: 1, pageSize: 10, status: "", total: 10 },
      // 切换筛选状态，不更新bug修复
      random: "",
      //   对话框显示
      dialogVisible: false,

      // 当前筛选的状态
      currentSelect: "",
      isEdit: false,
      // 动态生成下拉列表的筛选条件
      dynamicFilter: {},
      // 表格的数据
      tableList: [],
      tableModes: [
        {
          type: "selection",
          label: "编号",
        },
        {
          type: "index",
          label: "编号",
          mode: "index",
        },
        {
          label: "关键词",
          mode: "keyword",
        },
        {
          label: "近似词",
          mode: "synonym",
        },
      ],
      comForm: {
        modes: [
          {
            placeholder: "请输入铭感词",
            formType: "input",
            mode: "keyword",
            label: "关键词",
            value: "",
          },
        ],
      },
    };
  },

  mounted() {
    this.getSynonymListFn(this.paging);
  },
  methods: {
    ChangeIsEditFn() {
      this.isEdit = false;
    },
    // 修改
    async editFn(row) {
      await this.$refs.AddAndEdit.getDetailFn(row);
      this.dialogVisible = true;
      this.isEdit = true;
    },
    // 保存
    async addOrEditFn(data) {
      let result = null;
      console.log(data);
      if (data.id) {
        // 修改
        result = await getSynonymEditAPi(data);
      } else {
        // 新增
        result = await getSynonymAddAPi(data);
      }
      if (result.code == 200) {
        this.$modal.msgSuccess(result.msg);
        // 刷新列表
        this.getSynonymListFn();
        // 关闭对话框
        this.dialogVisible = false;
      }
    },
    // 重设函数
    resetFn() {},
    // 当前页发生改变时候
    CurrentPageChangeFn(pageNUm) {
      this.paging.pageNum = pageNUm;
      this.getSynonymListFn(this.paging);
    },
    // 页容量发生改变时候
    currentPageSizeChangeFn(pageSize) {
      this.paging.pageSize = pageSize;
      this.getSynonymListFn(this.paging);
    },
    // 筛选数据
    queryFn(arr) {
      arr.forEach((item) => {
        for (const key in item) {
          this.paging[key] = item[key];
        }
      });
      this.currentSelect = this.initSelectValue;

      this.paging.pageNum = 1;
      this.getSynonymListFn(this.paging);
    },
    // 获取列表数据
    async getSynonymListFn(pagination = null) {
      this.loading = true;
      let { rows, code, total } = await getSynonymListAPi(pagination);
      console.log(rows, "近义词返回的数据");
      if (code == 200) {
        this.tableList = rows;
        this.paging.total = total;
        // 把后端放回的字段变成下拉筛选字段

        this.loading = false;
      }
    },

    // 查看
    async deleteFn(row) {
      const { code, msg } = await getSynonymRemoveAPi(row.idStr);
      if (code == 200) {
        this.$modal.msgSuccess(msg);
        this.getSynonymListFn();
      }
    },
  },
};
</script>

<style lang ="scss" scoped>
.comform {
  margin-top: 20px;
}
</style>